package cn.lili.modules.grab.xdxt.reps.goods;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.hpsf.Decimal;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ListGoodsReps {

    private int code;
    private String msg;
    private Result result;
    private String traceId;

    @Data
    public static class Result {
        @ApiModelProperty("页码，不传默认从1页开始")
        private int current;
        @ApiModelProperty("分页大小，每页返回的数据条数，输入值范围1~100，不传本参数，默认为每页40条")
        private int total;
        @ApiModelProperty("查询对象")
        private List<Records> records;

        @Data
        public static class Records {
            @ApiModelProperty("商品id")
            private int id;
            @ApiModelProperty("商品标题")
            private String title;
            @ApiModelProperty("商品主图")
            private String mainImg;
            @ApiModelProperty("商品上下架状态\n" +
                    "0：下架\n" +
                    "1：上架")
            private int state;
            @ApiModelProperty("商品归属分类")
            private List<GoodsCategory> goodsCategoryList;
            @ApiModelProperty("商品规格")
            private List<Specification> specificationList;

            @Data
            public static class GoodsCategory {
                @ApiModelProperty("一级分类id")
                private int c1;
                @ApiModelProperty("二级分类id，为 0 则无二级分类")
                private int c2;
                @ApiModelProperty("三级分类id，为 0 则无三级分类")
                private int c3;
            }

            @Data
            public static class Specification {
                @ApiModelProperty("规格编码")
                private String code;
                @ApiModelProperty("规格名称")
                private String name;
                @ApiModelProperty("商品id")
                private String goodsId;
                @ApiModelProperty("规格信息，key为规格名，value为规格值")
                private String specInfo;
                @ApiModelProperty("会员成本价")
                private double currVipPrice;
                @ApiModelProperty("是规格主图路径")
                private String itemMainImg;
                @ApiModelProperty("规格图片路径数组")
                private List<String> imgs;
                @ApiModelProperty("规格详情图路径数组")
                private List<String> detailImgs;
                @ApiModelProperty("规格视频路径")
                private String video;
                @ApiModelProperty("禁发地区")
                private String forbidBuyArea;
                @ApiModelProperty("是否包邮\n" +
                        "0：不包邮\n" +
                        "1：包邮（部分地区会收取运费，收费地区价格详情见‘remoteAreaFreight’字段）")
                private int isFreeDelivery;
                @ApiModelProperty("部分地区运费json")
                private String remoteAreaFreight;
                @ApiModelProperty("市场价图标路径")
                private String marketIcon;
                @ApiModelProperty("市场价格")
                private double marketPrice;
                @ApiModelProperty("起订量，购买数量必须≥该值")
                private int minBuyNum;
                @ApiModelProperty("步增量，购买数量必须为起订量的倍数")
                private int stepNum;
                @ApiModelProperty("当前库存")
                private int stockNum;
                @ApiModelProperty("重量，单位kg")
                private double weight;
                @ApiModelProperty("体积，单位cm³")
                private double volume;
                @ApiModelProperty("基本单位")
                private String unit;
            }
        }
    }

    /**
     * 将JSON构建为ListGoodsReps
     *
     * @param jsonString 值
     */
    public static ListGoodsReps builderListGoodsReps(JSONObject jsonString) throws JsonProcessingException {
        //忽略这些未知字段
        ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);;
        ListGoodsReps listGoodsReps = mapper.readValue(jsonString.toString(), ListGoodsReps.class);
        return listGoodsReps;
    }
}
